package com.alibaba.nacos.naming.interceptor;

import com.alibaba.nacos.naming.myinterface.MyInterface;
import com.alibaba.nacos.naming.pojo.ServiceInfoLog;
import com.alibaba.nacos.naming.utils.KafkaUtil;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Aspect
@Component
@Configuration
public class RequestInterceptor implements HandlerInterceptor {

    private static final Logger log = LoggerFactory.getLogger(HandlerInterceptor.class);

    @Autowired
    private KafkaUtil kafkaUtil;

    @Value("${nacos.inetutils.ip-address}")
    private String serviceIp;

    /**
     * 以 controller 包下定义的所有请求为切入点
     */
    @Pointcut(value = "execution(public * com.alibaba.nacos.naming.controllers..*.*(..))")
    public void reqOpenAPILog() {
        log.info("start......");
    }
    /**
     * 在切点之前织入
     *
     * @param joinPoint
     * @throws Throwable
     */
//    @Before("reqOpenAPILog()")
//    public void doBefore(JoinPoint joinPoint) throws Throwable {
//    }

    /**
     * 在切点之后织入
     *
     * @throws Throwable
     */
//    @After("reqOpenAPILog()")
//    public void doAfter() throws Throwable {
//    }

    /**
     * 环绕
     *
     * @param proceedingJoinPoint
     * @return
     * @throws Throwable
     */
    @Around("reqOpenAPILog()")
    public Object doAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        try{
            ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
            if(attributes != null) {
                HttpServletRequest request = attributes.getRequest();
                HttpServletResponse response = attributes.getResponse();
                long startTime = System.currentTimeMillis();

                ServiceInfoLog infoLog = new ServiceInfoLog();
                infoLog.setServiceName("nacos");
                infoLog.setUrl(request.getRequestURL().toString());
                infoLog.setIp(serviceIp);
                infoLog.setParam("");
                infoLog.setClassMethod(proceedingJoinPoint.getSignature().getDeclaringTypeName() + "." + proceedingJoinPoint.getSignature().getName());
                if(response != null){
                    infoLog.setStatus(response.getStatus());
                }else {
                    infoLog.setStatus(-1);
                }

                Object result = proceedingJoinPoint.proceed();

                infoLog.setLongTime((System.currentTimeMillis() - startTime) * 1.0 / 1000);
                kafkaUtil.sendServiceCountMsg(infoLog.toString());
                return result;
            }else {
                return null;
            }
        }catch (Exception ex){
            log.error(ex.toString(),ex);
            return null;
        }
    }
}

